<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>导出Excel</title>
	</head>
	<body>
		<table id="myTable" border="1">
			<tr>
				<th>姓名</th>
				<th>年龄</th>
				<th>城市</th>
			</tr>
			<tr>
				<td>张三</td>
				<td>25</td>
				<td>北京</td>
			</tr>
			<tr>
				<td>李四</td>
				<td>30</td>
				<td>上海</td>
			</tr>
		</table>
		<button onclick="exportToExcel()">导出为Excel</button>

		<script>
			function exportToExcel() {
				var table = document.getElementById("myTable");
				var rows = table.rows;
				var cols = rows[0].cells;
				var csv = "";

				for (var i = 0; i < cols.length; i++) {
					csv += cols[i].innerText + (i < cols.length - 1 ? "," : "");
				}
				csv += "";

				for (var i = 1; i < rows.length; i++) {
					cols = rows[i].cells;
					for (var j = 0; j < cols.length; j++) {
						csv += cols[j].innerText + (j < cols.length - 1 ? "," : "");
					}
					csv += "";
				}

				var hiddenElement = document.createElement('a');
				hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
				hiddenElement.target = '_blank';
				hiddenElement.download = '导出的Excel文件.csv';
				hiddenElement.click();
			}
		</script>
	</body>
</html>